
public abstract class Unit extends Controllable{
	int facing;
	protected int targetX, targetY;
	boolean dead;
	
	protected static int[] range = {0,1,2,1};
	final int MELEE = 11, RANGED = 12, EXPLORER = 13, COLONIST = 14;

	
	public Unit(int x, int y){
		super(x,y);
		targetX = x;
		targetY = y;
		facing = 2;
		armor = 20;
		dead = false;
		canAttack = true;
		nutrient = metal = energy = 100;
		metalRT = metalORT = 5;
		nutrientRT = nutrientORT =  20;
		energyRT = energyORT = 10;
		maxHealth =health = 1000;
		
		this.offence = this.originalOffence = 20;
		this.defence = this.originalDefence = 20;
		
	}

	
	// ========= GETTERS =============
	public void face(int dir){facing = dir;}
	int attack(){return offence;} 	//doesn't actually attack, just returns the amount of attack. most structures attack is 0
	public String toString(){return this.getTypeName()+": "+this.getID()+" = "+health+" - Energy: "+energy+" Metal: "+metal+" Food: "+nutrient;}	

	
	// =========== SETTERS ============
	public void move(int _x, int _y){
		this.targetX = _x;
		this.targetY = _y;
	}
	
	int attack(int dir){
		face(dir);
		return offence;
	}
	
	// ========== DO STUFF ============
	
	public boolean die(){
		dead = true;
		return map.die(x, y, this);
		}
	
	int endTick() {
		int needed=0, total = metalRT + energyRT + nutrientRT;
		
		nextCommand();
		
		
		if(dead)
			return -1;
		
		if(!damage(damage)){
			die();
			return -1;
		}
		damage = 0;

		
		if(metal - metalRT >= 0)
			metal -= metalRT;
		else{
			needed += metalRT - metal;
			metal = 0;
		}
		
		if(energy - energyRT >= 0)
			energy -= energyRT;
		else{
			needed += energyRT - energy;
			energy = 0;
		}
		
		if(nutrient - nutrientRT >= 0)
			nutrient -= nutrientRT;
		else{
			needed += nutrientRT - nutrient;
			nutrient = 0;
		}
		if(needed > 0)
			if(damage((float)(needed)/total/2))
				return 0;
			else{
				die();
				return -1;
			}
		
		
		return 1;
	}
}
